package my.edu.model.information.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import my.edu.aspect.LogExecutionTime;
import my.edu.common.domain.PageDTO;
import my.edu.common.validate.IntegerPattern;
import my.edu.common.validate.groups.Insert;
import my.edu.common.domain.Response;
import my.edu.constant.CustomerPaymentTerms;
import my.edu.model.information.entity.CustomerReceivableBalanceDetail;
import my.edu.model.information.entity.dto.CustomerDTO;
import my.edu.model.information.entity.dto.CustomerSearchDTO;
import my.edu.model.information.entity.dto.SupplierSearchDTO;
import my.edu.model.information.service.ICustomerReceivableBalanceDetailService;
import my.edu.model.information.service.ICustomerService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/customer")
@RestController
@Tag(name = "客户接口")
@Validated
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private CustomerPaymentTerms customerPaymentTerms;
    @Autowired
    private ICustomerReceivableBalanceDetailService customerReceivableBalanceDetailService;


    @PutMapping
    @RequiresPermissions("customer:save")
    @Operation(summary = "新增客户")
    public Response save(@Validated(value = Insert.class) @RequestBody CustomerDTO customerDTO) {
        customerService.savenew(customerDTO);
        return Response.success();
    }


    @LogExecutionTime
    @GetMapping("/list")
    @RequiresPermissions("customer:get")
    @Operation(summary = "获取客户列表")
    public Response getCustomer(@ModelAttribute PageDTO pageDTO, CustomerSearchDTO customerSearchDTO) {
        return Response.success(customerService.listAll(pageDTO,customerSearchDTO));
    }

    @GetMapping
    @Operation(summary = "获取客户列表")
    public Response getCustomerOnly() {
        return Response.success(customerService.listAllOnly());
    }



    @GetMapping("/paymentTerms")
    @RequiresPermissions("customer:get")
    @Operation(summary = "获取客户付款条件列表")
    public Response getCustomerPaymentTerms() {
        return Response.success(customerPaymentTerms.getPaymentTerms());
    }


    @GetMapping("/one")
    @RequiresPermissions("customer:get")
    @Operation(summary = "根据编号获取客户信息")
    public Response getCustomerByOn( @NotBlank(message = "id不能为空") String id) {
        return Response.success(customerService.getVOById(id));
    }


    @PostMapping("/batchSetStatus")
    @RequiresPermissions("customer:update")
    @Operation(summary = "更新客户信息")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        if(status!=0&&status!=1){
            return Response.error("状态值错误");
        }
        List<Long> longs = StringUtils.strToLongList(ids);
        customerService.setStatus(longs,status);
        return Response.success();
    }

    @PostMapping("/update")
    @RequiresPermissions("customer:update")
    @Operation(summary = "更新客户信息")
    public Response update(@Valid @RequestBody CustomerDTO customerDTO) {
        customerService.updateByDTO(customerDTO);
        return Response.success();
    }


    @DeleteMapping("/delete")
    @RequiresPermissions("customer:del")
    @Operation(summary = "删除客户")
    public Response delete(@NotNull(message = "id不能为空")Long id) {
        customerService.removeById(id);
        customerReceivableBalanceDetailService
                .remove(Wrappers.<CustomerReceivableBalanceDetail>lambdaQuery()
                        .eq(CustomerReceivableBalanceDetail::getCustomer_id,id));
        return Response.success();
    }

    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("customer:del")
    @Operation(summary = "删除客户")
    @Transactional
    public Response deleteBatch(@NotNull(message = "id不能为空")String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        customerService.removeByIds(longs);
        customerReceivableBalanceDetailService
                .remove(Wrappers.<CustomerReceivableBalanceDetail>lambdaQuery()
                        .in(CustomerReceivableBalanceDetail::getCustomer_id,longs));
        return Response.success();
    }

}
